package com.zbkj.crmeb.order;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crmeb.core.common.CommonResult;
import com.zbkj.crmeb.enums.BarCodeEnum;
import com.zbkj.crmeb.store.model.StoreOrder;
import com.zbkj.crmeb.store.service.StoreOrderInfoService;
import com.zbkj.crmeb.store.vo.StoreOrderInfoVo;

@Service
public abstract class AbstractStoreOrderService {
    private static Logger logger = LoggerFactory.getLogger(AbstractStoreOrderService.class);
    @Autowired
    StoreOrderInfoService storeOrderInfoService;
    @Autowired
    StoreOrderFactory storeOrderFactory;


    /**
     * 推送上游订单
     *
     * @param
     * @return
     * @author cyz
     * @date 2021/3/14 14:42
     **/
    public abstract CommonResult<Object> addChannelOrder(StoreOrder storeOrder , List<StoreOrderInfoVo> orderInfoVoList);

    /**
     * 取消上游订单
     *
     * @param
     * @return
     * @author cyz
     * @date 2021/3/14 14:42
     **/
    public abstract CommonResult<Object> cancelChannelOrder(StoreOrder storeOrder, List<StoreOrderInfoVo> orderInfoVoList);

    /**
     * 拆单---推送上游订单
     *
     * @param
     * @return
     * @author cyz
     * @date 2021/3/14 14:42
     **/
    public void separateOrder(StoreOrder storeOrder) {
        List<StoreOrderInfoVo> orderInfoVoList = storeOrderInfoService.getOrderListByOrderId(storeOrder.getId());
        List<StoreOrderInfoVo> buyerOrderList = new ArrayList<>();
        List<StoreOrderInfoVo> europeOrderList = new ArrayList<>();
        for(StoreOrderInfoVo info : orderInfoVoList){
            String barCode = info.getInfo().getProductInfo().getBarCode();
            if (barCode.startsWith(BarCodeEnum.BUYER.getCode().toString())){
                buyerOrderList.add(info);

            } else if(barCode.startsWith(BarCodeEnum.EUROPE.getCode().toString())){
                europeOrderList.add(info);

            }
        }
        if(null!=buyerOrderList && buyerOrderList.size() > 0){
            //推送buyer订单
            logger.info("开始推送buyer订单，订单id:{} buyerOrderInfo数量:{}",storeOrder.getId(),buyerOrderList.size());
            storeOrderFactory.getStoreOrderService(BarCodeEnum.BUYER).addChannelOrder(storeOrder,buyerOrderList);
        }
        if(null!=europeOrderList && europeOrderList.size() > 0){
            //推送欧洲订单
            logger.info("开始推送europe订单，订单id:{} europeOrderInfo数量:{}",storeOrder.getId(),europeOrderList.size());
            storeOrderFactory.getStoreOrderService(BarCodeEnum.EUROPE).addChannelOrder(storeOrder,europeOrderList);
        }
    }

    public abstract CommonResult<Object> getOrderInfo(String channelOrderSn);


}
